#include "define.h"
#include "menu.h"
#include "save.h"
#include "music.h"

extern Menu *menu;

Menu *menu_init(int max_option, int init_x, int init_y,
 int col_space, int row_space, int state, int prev_state, ALLEGRO_BITMAP *prev_bitmap){
    Menu *menu = malloc(sizeof(Menu));
    menu->max_option = max_option;
    menu->init_x = init_x;
    menu->init_y = init_y;
    menu->row_space = row_space;
    menu->col_space = col_space;
    menu->index = 0;
    menu->state = state;
    menu->prev_state = prev_state;
    menu->prev_bitmap = prev_bitmap;
    return menu;
}

void menu_output(ALLEGRO_BITMAP *selection){
    int x = menu->init_x + menu->index * menu->col_space;
    int y = menu->init_y + menu->index * menu->row_space;
    al_draw_bitmap(selection, x, y, 0);
}

bool menu_update(ALLEGRO_EVENT *event){
    if(event->keyboard.keycode == ALLEGRO_KEY_UP)
    {
        play_select();
        menu->index--;
        if(menu->index < 0)
                menu->index = menu->max_option - 1;
    }
    if(event->keyboard.keycode == ALLEGRO_KEY_DOWN)
    {
        play_select();
        menu->index++;
        if(menu->index >= menu->max_option)
            menu->index = 0;
            
    }
    if(event->type == ALLEGRO_EVENT_MOUSE_AXES){
        int x = event->mouse.x;
        int y = event->mouse.y;
        for(int i = 0; i < menu->max_option; i++){
            if(x >= menu->init_x + i * menu->col_space && x <= menu->init_x + i * menu->col_space + 100){
                if(y >= menu->init_y + i * menu->row_space && y <= menu->init_y + i * menu->row_space + 100){
                    menu->index = i;
                    return 0;
                }
            }
        }
    }
    if(event->keyboard.keycode == ALLEGRO_KEY_ENTER)
    {
        play_confirm();
        return 1;
    }
    return 0;
}

void menu_jumpto(Game *game){
    switch(menu->state){
        case START_MENU:
            switch(menu->index){
                case 0:
                    play_start();
                    game_start(game);
                    break;
                case 1:
                    play_start();
                    game_start(game);
                    load_game();
                    break;
                case 2:
                    exit(0);
                    break;
                default:
                    break;
            }
            break;
        case PAUSE_MENU:
            switch(menu->index){
                case 0:
                    game->state = GAME_MODE_1;
                    game->background = al_load_bitmap("res/bg1.png");
                    break;
                case 1:
                    save_game();
                    menu_exit(game);
                    break;
                case 2:
                    game->state = START_MENU;
                    game->background = al_load_bitmap("res/bg.png");
                    menu = menu_init(3, 1385, 420, -100, 160, START_MENU, PAUSE_MENU, game->background);
                    break;
                default:
                    break;
            }
    }
}

void menu_exit(Game *game){
    game->state = menu->prev_state;
    game->background = menu->prev_bitmap;
}